package com.rt.code.controller;

import com.rt.tmpt.dto.Result;
import com.rt.tmpt.utils.JsonPaginationUtils;
import com.rt.tmpt.utils.StringUtils;
import com.rt.tmpt.utils.WebUtils;
import com.rt.tmpt.utils.report.ExcelUtils;
import com.rt.code.dto.A2userDto;
import com.rt.code.dto.PageDto;
import com.rt.code.service.intf.IA2userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户控制类")
@Controller
@RequestMapping("/admin123/coding/a2user")
public class A2userAction {

    @Resource
    private IA2userService a2userService;


    /**
     * 分页查询列表
     * @return
     */
    @GetMapping(value = "/a2userAjax_queryListBySearch", produces = "application/json; charset=utf-8")
    @ApiOperation("分页查询列表")
    @ResponseBody
    public Result queryListBySearch(@ModelAttribute A2userDto a2userDto,
                                    @ModelAttribute PageDto pageDto,
                                    HttpServletRequest request) {
        Result<A2userDto> result = new Result<>();
        try {

            //查询参数
            //分页/排序查询条件
            //默认主键desc
            pageDto.configDefaultValue();

            //查询条件
            a2userDto.setDeleteflag("0");
            A2userDto a2userParam = new A2userDto();
            a2userParam.setOwUserType(a2userDto.getOwUserType());
            a2userParam.setA2postId(a2userDto.getA2postId());
            String name = a2userDto.getName();
            a2userParam.setName(name != null ? "%" + name + "%" : null);
            String loginname = a2userDto.getLoginname();
            a2userParam.setLoginname(loginname != null ? "%" + loginname + "%" : null);
            a2userParam.setCreatedate_Start(a2userDto.getCreatedate_Start());
            a2userParam.setCreatedate_End(a2userDto.getCreatedate_End());
            a2userParam.setCreatedate(a2userDto.getCreatedate());

            //调用远程服务
            result = a2userService.pagination(a2userParam, pageDto);

        } catch (Exception e) {
            e.printStackTrace();
            result.fault("error:" + e.getLocalizedMessage());
        }
        return result;
    }

    @ApiOperation("")
    @GetMapping(value = "/selectById")
    @ResponseBody
    public Result selectById(Long id){
        Result<A2userDto> result = new Result<>();
        try {

            //调用远程服务
            result =  a2userService.selectById(id);

        } catch (Exception e) {
            e.printStackTrace();
            result.fault(e.getLocalizedMessage());
        }
        return result;
    }


    /**
     * 分页查询列表(没有数据边界)
     * @return
     */
    @GetMapping(value = "/a2userAjax_queryListBySearchNoDataLimit", produces = "application/json; charset=utf-8")
    @ApiOperation("分页查询列表（无数据边界控制）")
    @ResponseBody
    public Result<A2userDto> queryListBySearchNoDataLimit(@ModelAttribute A2userDto a2userDto,
                                                          @ModelAttribute PageDto pageDto,
                                                          HttpServletRequest request, HttpServletResponse response) {
        Result<A2userDto> resultObj = new Result<>();
        try {

            HttpSession session = request.getSession();

            //查询参数
            //分页/排序查询条件
            //默认主键desc
            pageDto.configDefaultValue();

            //查询条件
            A2userDto a2userParam = new A2userDto();
            a2userParam.setOwUserType(a2userDto.getOwUserType());
            a2userParam.setA2postId(a2userDto.getA2postId());
            String name = a2userDto.getName();
            a2userParam.setName(name != null ? "%" + name + "%" : null);
            a2userParam.setCreatedate_Start(a2userDto.getCreatedate_Start());
            a2userParam.setCreatedate_End(a2userDto.getCreatedate_End());
            a2userParam.setCreatedate(a2userDto.getCreatedate());

            //调用远程服务
            resultObj = a2userService.pagination(a2userParam, pageDto);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.fault("error:" + e.getLocalizedMessage());
        }
        return resultObj;
    }


    /**
     * 分页查询列表(没有数据边界)
     * @return
     */
    @GetMapping(value = "/a2userAjax_download", produces = "application/json; charset=utf-8")
    @ApiOperation("下载报表")
    @ResponseBody
    public String download(@ModelAttribute PageDto pageDto,
                           @ModelAttribute A2userDto a2userDto,
                           HttpServletRequest request, HttpServletResponse response) {
        try {
            pageDto.setPageCurrent(1);
            pageDto.setPageSize(100000000);

            HttpSession session = request.getSession();

            //查询参数
            //分页/排序查询条件
            //默认主键desc
            if (StringUtils.isNull(pageDto.getSortColumn())) {
                pageDto.setSortColumn("id");
                pageDto.setSortOrder("desc");
            }
            pageDto.setPageCurrent(pageDto.getPageCurrent() - 1);

            //查询条件
            String name = a2userDto.getName();
            a2userDto.setName(name != null ? "%" + name + "%" : null);

            //调用远程服务
            Result<A2userDto> resultObj = a2userService.pagination(a2userDto, pageDto);
            if (!resultObj.isSuccess()) {
                JsonPaginationUtils.responseWriter(response, "error:" + resultObj.getContent());
                return null;
            }
            Map<String, Object> resultMap = resultObj.getMap();

            //分页记录
            List<A2userDto> list = (List<A2userDto>) resultMap.get("rows");
            JSONArray jsonArrayRows = JSONArray.fromObject(list);

            //报表
            List<List<String>> rowList = new ArrayList<List<String>>();
            List<String> col = null;

            col = new ArrayList<String>();
            //col.add("ID");
            col.add("姓名");
            col.add("员工类型");
            col.add("性别");
            col.add("后台登录名");
            col.add("APP登录名");
            col.add("工号");
            col.add("联系方式");
            rowList.add(col);

            for (A2userDto dto : list) {
                col = new ArrayList<String>();
                //col.add(a2userDto.getId());
                col.add(dto.getName());
                col.add(dto.getSex_Str());
                col.add(dto.getLoginname());
                col.add(dto.getAppLoginname());
                col.add(dto.getJobNumber());
                col.add(dto.getPhone());
                rowList.add(col);
            }

            ExcelUtils.downloadReport("用户列表", rowList, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * Vue
     * 伪删除数据
     * @return
     */
    @PostMapping(value = "/a2userAjax_removeData")
    @ApiOperation("逻辑删除对象")
    @ResponseBody
    public Result removeData(String _userId, String id) {
        Result resultObj = new Result();
        try {
            //调用远程服务
            resultObj = a2userService.deleteById(Long.valueOf(id), _userId);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.fault("error:系统出错:" + e.getLocalizedMessage());
        }

        return resultObj;
    }


    /**
     * Vue
     * 编辑数据
     * @return
     */
    @ApiOperation("编辑对象")
    @PostMapping(value = "/a2userAjax_editData")
    @ResponseBody
    public Result editData(@RequestBody A2userDto a2userDto,
                           String _userId, String _userName) {
        Result<A2userDto> resultObj = new Result();
        try {
            //参数
            a2userDto.setModifymanId(_userId);
            a2userDto.setModifyman(_userName);
            a2userDto.setCompanyId("0");
            a2userDto.setAppLoginname(a2userDto.getLoginname());

            //调用远程服务
            resultObj = a2userService.editData(a2userDto);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.fault("error:系统出错:" + e.getLocalizedMessage());
        }
        return resultObj;
    }

    /**
     * Vue
     * 添加数据
     * @return
     */
    @PostMapping(value = "/a2userAjax_addData", produces = "application/json; charset=utf-8")
    @ApiOperation("增加对象")
    @ResponseBody
    public Result addData(@RequestBody A2userDto a2userDto,
                          String _userId, String _userName) {
        Result<A2userDto> resultObj = new Result();
        try {

            //插入参数
            a2userDto.setCreatemanId(_userId);
            a2userDto.setCreateman(_userName);
            a2userDto.setCompanyId("0");
            a2userDto.setAppLoginname(a2userDto.getLoginname());
            a2userDto.setCompanyId("1");

            //调用远程服务
            resultObj = a2userService.addData(a2userDto);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.fault("error:系统出错:" + e.getLocalizedMessage());
        }
        return resultObj;
    }


}
